package sk.stuba.fiit.ltasys.web.mvc.model.service.impl;

import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sk.stuba.fiit.ltasys.web.mvc.model.entity.ArchivePackageEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.entity.AuthoritiesEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.entity.BinaryDataEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.entity.CertificateEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.entity.CrlEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.entity.EvidenceRecordEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.entity.TimestampEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.entity.UserDataEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.entity.UserEntity;
import sk.stuba.fiit.ltasys.web.mvc.model.service.ArchiveService;
import sk.stuba.fiit.ltasys.web.mvc.model.service.CertificateService;
import sk.stuba.fiit.ltasys.web.mvc.model.service.CrlService;
import sk.stuba.fiit.ltasys.web.mvc.model.service.TestService;
import sk.stuba.fiit.ltasys.web.mvc.model.service.UserService;

@Service("testService")
public class TestServiceImpl implements TestService{

	@Autowired
	private UserService userService;
	
	@Autowired
	CrlService crlService;
	
	@Autowired
	CertificateService certificateService;
	
	@Autowired
	ArchiveService archiveService;
	
	public void XtestSaveCertificate(){
		String issuer = "TEST_CA";
		BigInteger serialNum = new BigInteger("12");
		byte[] data = new byte[]{0x23,0x54,0x23,0x65,0x63,0x72};
		
		CertificateEntity certificate = new CertificateEntity();
		certificate.setCert(data);
		certificate.setIssuer(issuer);
		certificate.setSerialNumber(serialNum);
		certificate.setValid_from(new Date());
		certificate.setValid_to(new Date());
		certificateService.saveCertificate(certificate );
	}
	
	public CertificateEntity XtestLoadCertificate() throws Exception{
		String issuer = "TEST_CA";
		BigInteger serialNum = new BigInteger("12");
		CertificateEntity certificate = certificateService.handleCertificate(serialNum);
		return certificate;
	}
	
	public void XtestDeleteCertificate(CertificateEntity certificate){
		certificateService.deleteCertificate(certificate);
	}
	
	public void XtestSaveCrl(){
		String issuer = "TEST_CA";
		BigInteger serialNum = new BigInteger("12");
		byte[] data = new byte[]{0x23,0x54,0x23,0x65,0x63,0x72};

		CrlEntity crl = new CrlEntity();
		crl.setCrl(data);
		crl.setDownloadTime(new Date());
		crl.setIssuer(issuer);
		crl.setUpdateTime(new Date());
		crlService.saveCrl(crl );
	}
	
	public CrlEntity XtestLoadCrl() throws Exception{
		String issuer = "TEST_CA";
		BigInteger serialNum = new BigInteger("12");
		CrlEntity crl= null;
		HashSet<String> crls = new HashSet<String>();
		crls.add(""+serialNum);
		crl = crlService.handleCrls(crls);
		return crl;
	}
	
	 private void XtestdeleteCrl(CrlEntity crl) {		
			crlService.deleteCrl(crl);
	}
	 
	public void XtestAllCertRefEntity() throws Exception {
		XtestSaveCertificate();
		CertificateEntity certificate = XtestLoadCertificate();
		XtestDeleteCertificate(certificate);
		XtestSaveCrl();
		CrlEntity crl = XtestLoadCrl();	
		XtestdeleteCrl(crl);
	}
	
   public void XtestAllUserEntity(){
	   XtestSaveUser();
//	   UserEntity user = XtestLoadUser();
//	   XtestDeleteUser(user);
   }

	public void XtestDeleteUser(UserEntity user) {
		userService.delete(user);
}

	private UserEntity XtestLoadUser() {
	String username = "marek";
	UserEntity result = null;
	result = userService.findUserByUserName(username);
	return result;
}

	private void XtestSaveUser() {
		UserEntity personEntity = new UserEntity();
    	String name = "marek";
    	try {
	    	List<AuthoritiesEntity> au = new ArrayList<AuthoritiesEntity>();
	    	AuthoritiesEntity rola = new AuthoritiesEntity();
	    	rola.setAuthority(AuthoritiesEntity.ROLE_ADMIN);
	    	AuthoritiesEntity rola2 = new AuthoritiesEntity();
	    	rola2.setAuthority(AuthoritiesEntity.ROLE_USER);
	    	au.add(rola);
	    	au.add(rola2);
	    	personEntity.setAuthority(au);
	    	personEntity.setCity("Bratislava");
	    	personEntity.setCountry("Slovensko");
	    	personEntity.setDateOfBirth(new GregorianCalendar(1987,1,23).getTime());
	    	personEntity.setEmail("soper07@gmail.com");
	    	personEntity.setEnabled(true);
	    	personEntity.setOrganization("Horna dolna");
			personEntity.setPassword(personEntity.calculatePassword("hruska"));
			personEntity.setStreet("Zavodna 3B");
			personEntity.setTelNumber("0908903859");
			personEntity.setUsername(name);
			userService.save(personEntity);		
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}    	    		
	}

	
	public void XtestAllArchivePackageEntity() throws Exception{
		XtestSaveArchivePackage();
		ArchivePackageEntity prchPack = XtestLoadArchivePackage();
		

		Set<CertificateEntity> certificates = new HashSet<CertificateEntity>();
		Set<CrlEntity> crls = new HashSet<CrlEntity>();
//		XtestSaveCertificate();
		certificates.add(XtestLoadCertificate());
//		XtestSaveCrl();
		crls.add(XtestLoadCrl());
		
//		EvidenceRecordEntity e2= new EvidenceRecordEntity();
//		e2.setCertificates(certificates);
//		e2.setCrls(crls);
//		TimestampEntity ts2 = new TimestampEntity();
//		ts2.setArchiveRecord(prchPack);
//		ts2.setGenTimestamp(new Date());
//		ts2.setIssuer("TEST ISSUER");
//		ts2.setLeaf(false);
//		ts2.setTimestamp( new byte[]{0x21,0x42});
//		ts2.setValidFrom(new Date());
//		ts2.setValidTil(new Date());
//		e2.setTimestamp(ts2);
//		prchPack.getArchiveRecord().add(e2);
//		archiveService.saveArchivePackage(prchPack);
		
//		XtestDeleteArchivePackage(prchPack);
		
	}
	
	private void XtestDeleteArchivePackage(ArchivePackageEntity prchPack) {
		archiveService.deleteArchivePackage(prchPack);
		
	}

	public void XtestSaveArchivePackage(ArchivePackageEntity archivePackage) {
		archiveService.saveArchivePackage(archivePackage);
	}
	
	public ArchivePackageEntity XtestLoadArchivePackage(String token) {
		ArchivePackageEntity result = null;
		result = archiveService.loadArchivePackageByToken(token);
		return result;
	}
	
	private ArchivePackageEntity XtestLoadArchivePackage() {
		String token = "xxx-123-yyy-456-zzz-789";
		ArchivePackageEntity result = null;
		result = archiveService.loadArchivePackageByToken(token);
		return result;
	}

	private void XtestSaveArchivePackage() throws Exception {
		ArchivePackageEntity arcPackage = new ArchivePackageEntity();
		
		Set<CertificateEntity> certificates = new HashSet<CertificateEntity>();
		Set<CrlEntity> crls = new HashSet<CrlEntity>();
		XtestSaveCertificate();
		certificates.add(XtestLoadCertificate());
		XtestSaveCrl();
		crls.add(XtestLoadCrl());
		
		Set<EvidenceRecordEntity> archiveRecord = new HashSet<EvidenceRecordEntity>();
		EvidenceRecordEntity e1 = new EvidenceRecordEntity();
		e1.setCertificates(certificates);
		e1.setCrls(crls);
		
		TimestampEntity ts1 = new TimestampEntity();
		ts1.setArchiveRecord(arcPackage);
		ts1.setGenTimestamp(new Date());
		ts1.setIssuer("TEST ISSUER1");
		ts1.setLeaf(false);
		ts1.setTimestamp( new byte[]{0x21,0x42});
		ts1.setValidFrom(new Date());
		ts1.setValidTil(new Date());
		e1.setTimestamp(ts1);
		archiveRecord.add(e1);
		
		
	
		
		EvidenceRecordEntity e3= new EvidenceRecordEntity();
		e3.setCertificates(certificates);
		e3.setCrls(crls);
		
		TimestampEntity ts3 = new TimestampEntity();
		ts3.setArchiveRecord(arcPackage);
		ts3.setGenTimestamp(new Date());
		ts3.setIssuer("TEST ISSUER2");
		ts3.setLeaf(false);
		ts3.setTimestamp( new byte[]{0x21,0x42});
		ts3.setValidFrom(new Date());
		ts3.setValidTil(new Date());
		e3.setTimestamp(ts3);
		archiveRecord.add(e3);
		arcPackage.setArchiveRecord(archiveRecord);
		
		String description = "Zmluva s NBUSR";
		arcPackage.setDescription(description);
		
		String name = "Kupna zmluva";
		arcPackage.setName(name);
		
		String token = "xxx-123-yyy-456-zzz-789";
		arcPackage.setToken(token);
		
		XtestSaveUser();
		UserEntity user = XtestLoadUser();
		arcPackage.setUser(user);
		
		UserDataEntity userData = new UserDataEntity();
		userData.setSignature("<Signature>tu bude ulozeny popdis Jozko Mrkva</Signature>");		
		BinaryDataEntity bde1 = new BinaryDataEntity();
		byte[] file1= new byte[]{0x21,0x42};
		bde1.setData(file1);	
		BinaryDataEntity bde2 = new BinaryDataEntity();
		byte[] file2= new byte[]{0x21,0x42};
		bde2.setData(file2);
		List<BinaryDataEntity> udata = new ArrayList<BinaryDataEntity>(2);
		udata.add(bde1);
		udata.add(bde2);
		userData.setData(udata);	
		arcPackage.setUserData(userData);
		archiveService.saveArchivePackage(arcPackage);
	}

}
